package com.isoftstone.merchant.controller.operator;

import com.isoftstone.bank.controller.base.BaseController;
import com.isoftstone.common.Page;
import com.isoftstone.common.Tidings;
import com.isoftstone.common.service.ISysUserRoleService;
import com.isoftstone.merchant.entity.operator.MOperator;
import com.isoftstone.merchant.entity.role.PRoleUserKey;
import com.isoftstone.merchant.service.mdept.IMdeptService;
import com.isoftstone.merchant.service.operator.IMOperatorService;
import com.isoftstone.merchant.service.role.IPRoleService;
import com.isoftstone.merchant.service.role.IPRoleUserService;
import com.isoftstone.util.Const;
import com.isoftstone.util.PageData;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @Author: zhaojg
 * @Description
 * @Date: Created in 18:40 2017/11/27
 * @Modified by:
 */
@Controller
public class MOperatorController extends BaseController {

    @Autowired
    IMOperatorService operatorService;

    @Autowired
    ISysUserRoleService sysUserRoleService;

    @Autowired
    IPRoleService pRoleService;

    @Autowired
    IMdeptService mdeptService;

    /**
     * 显示商户端用户信息列表
     *
     * @param page
     * @return
     */
    @RequestMapping("/system/merchant/getOperatorlistPage")
    public ModelAndView getOperatorlist(Page page) {
        ModelAndView md = this.getModelAndView();
        PageData pd = this.getPageData();
        page.setPd(pd);
        try {
            List<MOperator> operatorList = operatorService.getOperatorlistPage(page);
            md.addObject("operatorList", operatorList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        md.setViewName("merchant/operator");
        return md;
    }

    /**
     * 新增商户端操作员
     */
    @RequiresPermissions("empadd")
    @RequestMapping("/system/merchant/saveOperator")
    @ResponseBody
    public Object saveOperator() {
        Tidings<String> tidings = new Tidings<String>();


        return tidings;
    }

    /**
     * 查看商户端操作员详情
     */
    @RequiresPermissions("empLook")
    @RequestMapping("/system/merchant/operatorShow")
    public ModelAndView getOperatorShow(String comUserCode) {
        ModelAndView md = new ModelAndView();
        md.setViewName("system/user/user_show");
        return md;
    }

    /**
     * 删除商户端操作员
     */
    @RequestMapping("/system/merchant/deleteOperator")
    @ResponseBody
    public Object deleteOperator(String operatorId) {
        Tidings<String> tidings = new Tidings<String>();
        try {
            //删除操作员表
            operatorService.deleteByPrimaryKey(operatorId);
            //根据用户id查询用户角色关联表
            List<PRoleUserKey> pRoleUserKeyList = sysUserRoleService.getroleIdByOperatorId(operatorId);
            for (PRoleUserKey pRoleUserKey : pRoleUserKeyList) {
                Integer roleId = pRoleUserKey.getRoleId();
                //删除角色表
                pRoleService.deleteRole(roleId);
            }
            //删除角色用户关联表
            sysUserRoleService.deleteUserRole(operatorId);
            tidings.setStatus(Const.SUCCESS);
            tidings.setMsg("删除成功！");
        } catch (Exception e) {
            tidings.setStatus(Const.SUCCESS);
            tidings.setMsg("删除失败！");
        }
        return tidings;
    }
}
